package com.qf.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsStorageMapper;
import com.qf.pojo.DtsStorage;
import com.qf.service.DtsStorageService;
import com.qf.util.AliyunOSSUtil;
import com.qf.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;


@Service
public class DtsStorageServiceImpl implements DtsStorageService {

    @Autowired
    private DtsStorageMapper dtsStorageMapper;

    //服务器地址
    @Value("${aliyun.oss.endpoint}")
    private String endpoint;
    //子账户名
    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;
    //子账户密码
    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;
    //桶名字
    @Value("${aliyun.oss.bucketName}")
    private String bucketName;


    /**
     * 查询对象存储列表
     * @param page
     * @param limit
     * @param key
     * @param name
     * @param sort
     * @param order
     * @return
     */
    @Override
    public IPage<DtsStorage> selectStorageList(Integer page, Integer limit, String key, String name, String sort, String order) {

        Page<DtsStorage> iPage = new Page<>(page, limit);

        QueryWrapper<DtsStorage> wrapper = new QueryWrapper<>();
        wrapper.eq("deleted", false);
        wrapper.orderByAsc("asc".equalsIgnoreCase(order),sort);
        wrapper.orderByDesc("desc".equalsIgnoreCase(order),sort);
        wrapper.like(!StringUtils.isEmpty(key), "`key`", key);
        wrapper.like(!StringUtils.isEmpty(name), "name", name);

        IPage<DtsStorage> data = dtsStorageMapper.selectPage(iPage, wrapper);
        return data;
    }

    /**
     * 上传阿里云并保存数据库
     * @param filename
     * @param size
     * @param ext
     * @param bytes
     * @return
     * @throws Exception
     */
    @Override
    public DtsStorage add(String filename, Integer size, String ext, byte[] bytes) throws Exception{

        //随机数，拼接文件名
        String key = UUID.randomUUID().toString();

        //上传阿里云
        String url = AliyunOSSUtil.uploadImage(endpoint, accessKeyId, accessKeySecret, bucketName, key + ext, bytes);

        DtsStorage storage = new DtsStorage();

        storage.setDeleted(false);
        storage.setAddTime(new Date());
        storage.setUpdateTime(new Date());
        storage.setKey(key+ext);
        storage.setName(filename);
        storage.setSize(size);
        storage.setUrl(url);
        storage.setType(ext);

        dtsStorageMapper.insert(storage);

        return storage;
    }

    /**
     * 修改对象  文件
     * @param dtsStorage
     * @return
     */
    @Override
    public Integer update(DtsStorage dtsStorage) {

        //逻辑删除
        dtsStorage.setUpdateTime(new Date());
        int result = dtsStorageMapper.updateById(dtsStorage);

        //删除阿里云
        //AliyunOSSUtil.deleteFile(endpoint, accessKeyId, accessKeySecret, bucketName, dtsStorage.getKey());

        // 删除数据库
        //int result = dtsStorageMapper.deleteById(dtsStorage.getId());

        return result;
    }
}
